package br.com.fatene.poo2.view;

import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.InputVerifier;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import br.com.fatene.poo2.controller.ClienteController;
import br.com.fatene.poo2.exceptions.BancoException;
import br.com.fatene.poo2.model.Cliente;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class CadastroClienteForm extends JDialog{
	private JFrame frame;

	private static final long serialVersionUID = 1L;
    private JPanel jContentPane = null;
    private JLabel lblSexo = null;
    private JLabel lblNome = null;
    private JLabel lblCPF = null;
    private JLabel lblData = null;
    private JLabel lblTipoLogradouro = null;
    private JLabel lblLogradouro = null;
    private JLabel lblNumeroLogradouro = null;
    private JLabel lblCepLogradouro = null;
    private JLabel lblAgencia = null;
    private JLabel lblNumero = null;
    private JLabel lblSaldo = null;
    
    private JTextField txtId = new JTextField();
    private JTextField txtNome = new JTextField();
    private JTextField txtCPF = new JTextField();
    private JTextField txtData = new JTextField();
	private JComboBox cmbSexo = new JComboBox(new String[] {"M", "F"});
    private JTextField txtTipoLogradouro = new JTextField();
    private JTextField txtLogradouro = new JTextField();
    private JTextField txtNumeroLogradouro = new JTextField();
    private JTextField txtCepLogradouro = new JTextField();

    private JTextField txtAgencia = new JTextField();
    private JTextField txtNumero = new JTextField();
    private JTextField txtSaldo = new JTextField();
    
    
    private JPanel pnlButtons = null;
    private JButton btnOk = null;
    private JButton btnExcluir = null;
    private JButton btnCancelar = null;
    protected boolean okSelecionado;
    private Cliente cliente = new Cliente();
    private ClienteController clienteController;

    public CadastroClienteForm(Frame owner,Cliente cli)
    {
        super(owner);
        cliente = new Cliente();
        this.cliente = cli;
        prepareUpdate();
        initialize();
    }

    public CadastroClienteForm()
    {
        initialize();
    }

    private void initialize(){
        
        frame = new JFrame();
		frame.setBounds(200, 200, 550, 400);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setTitle("Cadastro de Clientes");
		frame.setContentPane(getJContentPane());
        frame.setVisible(true);
    }

    private void prepareUpdate() {
    	txtId = new JTextField();
        txtNome = new JTextField();
        txtCPF = new JTextField();
        txtData = new JTextField();
        txtTipoLogradouro = new JTextField();
        txtLogradouro = new JTextField();
        txtNumeroLogradouro = new JTextField();
        txtCepLogradouro = new JTextField();

		txtAgencia = new JTextField();
		txtNumero = new JTextField();
		txtId.setText(String.valueOf(cliente.getId()));
		txtNome.setText(cliente.getNome());
		txtCPF.setText(cliente.getCpf());
		txtCPF.setEditable(false);
		txtData.setText(cliente.getDataNacimento());
		txtTipoLogradouro.setText(cliente.getTipoLogradouro());
		txtLogradouro.setText(cliente.getLogradouro());
		txtNumeroLogradouro.setText(cliente.getNumeroLogradouro());
		txtCepLogradouro.setText(cliente.getCepLogradouro());
		txtAgencia.setText(String.valueOf(cliente.getConta().getAgencia()));
		txtNumero.setText(String.valueOf(cliente.getConta().getNumero()));
		txtSaldo.setText(String.valueOf(cliente.getConta().getSaldo()));
	
	}

	//Montando os campos da tela
    private JPanel getJContentPane(){
    	
    	jContentPane = new JPanel();
    	
    	lblNome = new JLabel();
    	lblSexo = new JLabel();
    	lblCPF = new JLabel();
    	lblData = new JLabel();
    	lblTipoLogradouro = new JLabel();
    	lblLogradouro = new JLabel();
    	lblNumeroLogradouro = new JLabel();
    	lblCepLogradouro = new JLabel();
    	lblAgencia = new JLabel();
    	lblNumero = new JLabel();
    	lblSaldo = new JLabel();

    	lblNome.setText("Nome*:");
        lblSexo.setText("Sexo*:");
        lblCPF.setText("CPF*:");
        lblData.setText("Data de Nacimento*:");
        lblTipoLogradouro.setText("Tipo Logradouro(Rua, Av)*:");
        lblLogradouro.setText("Logradouro*:");
        lblNumeroLogradouro.setText("Numero*:");
        lblCepLogradouro.setText("CEP*:");
        lblAgencia.setText("Ag�ncia*:");
        lblNumero.setText("N�mero*:");
        lblSaldo.setText("Saldo*:");
        
        jContentPane.setLayout(new GridLayout(14,1));
        jContentPane.add(lblNome);
        jContentPane.add(txtNome);
        jContentPane.add(lblSexo);
        jContentPane.add(cmbSexo);
        jContentPane.add(lblCPF);
        jContentPane.add(txtCPF);
        jContentPane.add(lblData);
        jContentPane.add(txtData);
        jContentPane.add(lblTipoLogradouro);
        jContentPane.add(txtTipoLogradouro);
        jContentPane.add(lblLogradouro);
        jContentPane.add(txtLogradouro);
        jContentPane.add(lblNumeroLogradouro);
        jContentPane.add(txtNumeroLogradouro);
        jContentPane.add(lblCepLogradouro);
        jContentPane.add(txtCepLogradouro);
        jContentPane.add(lblAgencia);
        jContentPane.add(txtAgencia);
        jContentPane.add(lblNumero);
        jContentPane.add(txtNumero);

        jContentPane.add(lblSaldo);
        jContentPane.add(txtSaldo);
        
        jContentPane.add(getPnlButtons());
        
        return jContentPane;
        
    }

    //Bot�es
    private JPanel getPnlButtons(){
        if (pnlButtons == null){
        	
            FlowLayout flowLayout = new FlowLayout();
            flowLayout.setAlignment(FlowLayout.LEFT);
            flowLayout.setVgap(0);
            flowLayout.setHgap(3);
            pnlButtons = new JPanel();
            pnlButtons.setLayout(flowLayout);
            pnlButtons.add(getBtnOk(), null);
            pnlButtons.add(getBtnCancelar(), null);
            pnlButtons.add(getBtnExcluir(), null);
        }
        return pnlButtons;
    }

    private JButton getBtnExcluir() {
    	 if (btnExcluir == null)
         {
    		 btnExcluir = new JButton();
    		 btnExcluir.setText("Excluir");
    		 btnExcluir.addActionListener(new java.awt.event.ActionListener()
             {

				public void actionPerformed(java.awt.event.ActionEvent e)
                 {
					clienteController = new ClienteController(); 
                     try {
                    	 clienteController.delete(cliente);
 						JOptionPane.showMessageDialog(getFocusOwner(), "Cliente deletado com sucesso!"); 
 						new PesquisaClienteForm();
 						frame.setVisible(false);
 					} catch (Exception e1) {
 						JOptionPane.showMessageDialog(getFocusOwner(), "Erro ao exluir ou o cliente possui saldo!");
 						e1.printStackTrace();
 					}
                     
                     okSelecionado = true; 
                     setVisible(false);
                 }
             });
         }
         return btnExcluir;
     }

	//A��o bot�o OK
    private JButton getBtnOk()
    {
        if (btnOk == null)
        {
            btnOk = new JButton();
            btnOk.setText("Ok");
            btnOk.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					try {
						if(cliente != null && cliente.getId()<=0){
	                		cliente = new Cliente();
	                	}
	                	
						if(txtNome.getText().isEmpty() 
	                			|| cmbSexo.getSelectedItem().toString().isEmpty()
	                			|| txtCPF.getText().isEmpty()
	                			|| txtData.getText().isEmpty()
	                			|| txtTipoLogradouro.getText().isEmpty()
	                			|| txtLogradouro.getText().isEmpty()
	                			|| txtNumeroLogradouro.getText().isEmpty()
	                			|| txtCepLogradouro.getText().isEmpty()
	                			|| txtAgencia.getText().isEmpty()
	                			|| txtNumero.getText().isEmpty()
	                			|| txtSaldo.getText().isEmpty()){
		                		
							throw new BancoException("preencha os Campos Obrigat�rios!");
		                	}
						
	                	clienteController = new ClienteController(); 
	                	
	                    cliente.setNome(txtNome.getText());
	                    cliente.setSexo(cmbSexo.getSelectedItem().toString());
	                    cliente.setCpf(txtCPF.getText());
	                    cliente.setDataNacimento(txtData.getText());
	                    cliente.setTipoLogradouro(txtTipoLogradouro.getText());
	                    cliente.setLogradouro(txtLogradouro.getText());
	                    cliente.setNumeroLogradouro(txtNumeroLogradouro.getText());
	                    cliente.setCepLogradouro(txtCepLogradouro.getText());
	                    cliente.getConta().setAgencia(Integer.parseInt(txtAgencia.getText()));
	                    cliente.getConta().setNumero(Integer.parseInt(txtNumero.getText()));
	                    cliente.getConta().setSaldo(Double.valueOf(txtSaldo.getText()));
                    
                    
						clienteController.salvar(cliente);
						JOptionPane.showMessageDialog(getFocusOwner(), "Cliente salvo com sucesso!"); 
						new PesquisaClienteForm();
						frame.setVisible(false);
					} catch (SQLException e1) {
						JOptionPane.showMessageDialog(getFocusOwner(), "Erro ao gravar!");
						e1.printStackTrace();
					} catch (BancoException e1) {
						JOptionPane.showMessageDialog(getFocusOwner(), e1.getMessage());
					}
                    
                    okSelecionado = true; 
                    setVisible(false);
					
				}
			});
        }
        return btnOk;
    }

    //A��o botao CANCELAR
    private JButton getBtnCancelar()
    {
        if (btnCancelar == null)
        {
            btnCancelar = new JButton();
            btnCancelar.setText("Cancelar");
            btnCancelar.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					new PesquisaClienteForm();
    				frame.setVisible(false);
					
				}
			});
        }
        return btnCancelar;
    }
    
    public boolean alterarDados()    
    {        
        okSelecionado = false;  
        setModal(true);         
        setVisible(true);       
        return okSelecionado;   
    }
    
    public int getNome()
    {
        return Integer.parseInt(txtNome.getText());        
    }
    
    public String getSexo()
    {
        return cmbSexo.getSelectedItem().toString(); 
    }

}


